package com.emellend.api11.media;

import com.emellend.api11.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 播放音频
 * @author tiangh
 *
 */
public class MediaPlayerDemo_Audio extends Activity {
	private static final String TAG = "MediaPlayerDemo";
	private TextView tx;
	private String path="";
	private MediaPlayer mMediaPlayer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tx = new TextView(this);
		setContentView(tx);
		Bundle extras = getIntent().getExtras();
		playAudio(extras.getInt(MediaPlayerDemo.MEDIA));
	}

	private void playAudio(int MEDIA) {
		try {
			switch (MEDIA) {
			case MediaPlayerDemo.LOCAL_AUDIO:
				//在这里设置本地视频文件的地址
				path = "";
			    if (path == "") {
			        Toast.makeText(this,"请设置好本地的音频文件地址",Toast.LENGTH_LONG).show();
			    }
			    mMediaPlayer=new MediaPlayer();
			    mMediaPlayer.setDataSource(path);
			    mMediaPlayer.prepare();
			    mMediaPlayer.start();
				break;
			case MediaPlayerDemo.RESOURCES_AUDIO:
				mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
                mMediaPlayer.start();
				break;
			}
		} catch (Exception e) {
			Log.e(TAG, "error: " + e.getMessage(), e);
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
	}
}
